<!--
  module - 用户与权限--- 添加及编辑角色

  @author wuh 2020-05-13
-->
<template>
  <div class="mixin-components-container">
    <el-col :span="24">
      <el-card>

        <div slot="header" class="clearfix">
          <template>
            <el-page-header :content="titleContent" @back="goBack" />
          </template>
        </div>
        <div>
          <div>
            <div style="margin-bottom:50px;">
              <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="140px"
                style="padding: 25px 0;"
              >
                <el-row :gutter="20">
                  <el-col :span="8">
                    <el-form-item prop="roleName" label="角色名称">
                      <el-input
                        v-model="form.roleName"
                        placeholder="请填写角色名称"
                        maxlength="50"
                        show-word-limit
                        clearable
                      />
                    </el-form-item>
                    <el-form-item prop="sort" label="排序值">
                      <el-input
                        v-model="form.sort"
                        placeholder="请填写排序值"
                        maxlength="11"
                        show-word-limit
                        clearable
                        oninput="value=value.replace(/[^\d]/g,'')"
                      />
                    </el-form-item>
                    <el-form-item prop="isAdmin" label="是否是超级管理员">
                      <el-switch v-model="form.isAdmin" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
              <el-row style="padding-left:140px;">
                <el-link waves type="primary" @click="submitForm('form')">保存</el-link>
                <el-link waves type="info" @click="deleteRole">删除</el-link>
                <el-link waves type="info" @click="goBack">取消</el-link>
              </el-row>
            </div>
          </div>
        </div>

      </el-card>
    </el-col>
  </div>
</template>

<script>
// api
import { queryRole, saveRole, updateRole, deleteRole } from '@/api/userAndRole/sysRightsRole.js'
// 水波纹插件
import waves from '@/directive/waves'

export default {
  name: 'SysUser',
  directives: { waves },
  data() {
    return {
      listLoading: false,
      titleContent: '',
      form: {
        roleId: '',
        roleName: '',
        sort: 0,
        isAdmin: false
      },
      rules: {
        roleId: [
          { required: true, message: '请输入编码名称', trigger: 'blur' }
        ],
        roleName: [
          { required: true, message: '请输入编码', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入编码', trigger: 'blur' }
        ],
        isAdmin: [
          { required: true, message: '请输入编码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    const curVue = this
    if (curVue.$route.query.type === '1') {
      curVue.titleContent = '新建角色'
    } else if (curVue.$route.query.type === '2') {
      curVue.titleContent = '编辑角色'
      curVue.form.roleId = curVue.$route.query.data
      queryRole({ roleId: curVue.form.roleId }).then(res => {
        if (res.code === 1) {
          curVue.form.roleId = res.data.roleId
          curVue.form.roleName = res.data.name
          curVue.form.sort = res.data.sort
          curVue.form.isAdmin = res.data.isAdmin
        } else {
          this.noticeUtil.errorMsg('操作失败!')
        }
      })
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    submitForm(formName) {
      const formObj = {
        roleId: this.form.roleId,
        name: this.form.roleName,
        sort: this.form.sort,
        isAdmin: this.form.isAdmin
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.$route.query.type === '1') {
            saveRole(formObj).then(res => {
              if (res.code === 1) {
                console.log(1)
                this.noticeUtil.successMsg('操作成功!')
                this.$router.go(-1)
              } else {
                this.noticeUtil.errorMsg('操作失败!')
              }
            })
          } else {
            updateRole(formObj).then(res => {
              if (res.code === 1) {
                console.log(1)
                this.noticeUtil.successMsg('操作成功!')
                this.$router.go(-1)
              } else {
                this.noticeUtil.errorMsg('操作失败!')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    deleteRole() {
      const curVue = this
      const formObj = {
        roleId: this.form.roleId
      }
      const mes = '您确定要删除此角色吗?'
      curVue.noticeUtil.confirm(mes).then(() => {
        deleteRole(formObj).then(res => {
          if (res.code === 1) {
            curVue.noticeUtil.successMsg('操作成功！')
            curVue.$router.go(-1)
          }
        })
      }).catch(() => {
        curVue.noticeUtil.warningMsg('已取消操作！')
      })
    }
  }
}
</script>
